// @author Tjzlo
// @version 2025/2/21 09:55

package com.old.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.old.entity.Activity;
import com.old.entity.Post;
import com.old.entity.Video;
import com.old.mapper.ActivityMapper;
import com.old.mapper.AdminMapper;
import com.old.mapper.PostMapper;
import com.old.mapper.VideoMapper;
import com.old.result.PageBean;
import com.old.service.AdminService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AdminServiceImpl implements AdminService {

    private final AdminMapper adminMapper;
    private final PostMapper postMapper;
    private final VideoMapper videoMapper;
    private final ActivityMapper activityMapper;

    @Override
    public PageBean getPostList(Integer pageNo, Integer pageSize, Integer sortBy, String searchTerms) {
        PageHelper.startPage(pageNo, pageSize);
        Page<Post> page = adminMapper.getPostList(sortBy, searchTerms);

        long total = page.getTotal();
        List<Post> records = page.getResult();

        return new PageBean(total, records);
    }

    @Override
    public PageBean getVideoList(Integer pageNo, Integer pageSize, Integer sortBy, String searchTerms) {
        PageHelper.startPage(pageNo, pageSize);
        Page<Video> page = adminMapper.getVideoList(sortBy, searchTerms);

        long total = page.getTotal();
        List<Video> records = page.getResult();

        return new PageBean(total, records);
    }

    @Override
    public PageBean getActivityList(Integer pageNo, Integer pageSize, Integer sortBy, String searchTerms) {
        PageHelper.startPage(pageNo, pageSize);
        Page<Activity> page = adminMapper.getActivityList(sortBy, searchTerms);

        long total = page.getTotal();
        List<Activity> records = page.getResult();

        return new PageBean(total, records);
    }

    //审核帖子
    @Override
    @Transactional
    public void updatePostStatus(Integer postId) {
        //获取列表中已确定帖子存在+状态为审核中
        //直接在sql中更改状态
        postMapper.updatePostStatus(postId);
    }

    //审核视频
    @Override
    @Transactional
    public void updateVideoStatus(Integer videoId) {
        //获取列表中已确定视频存在+状态为审核中
        //直接在sql中更改状态
        videoMapper.updateVideoStatus(videoId);
    }

    //审核活动
    @Override
    public void updateActivityStatus(Integer activityId) {
        //获取列表中已确定活动存在+状态为审核中
        //直接在sql中更改状态
        activityMapper.updateActivityStatus(activityId);
    }

    //内容管理-post
    @Override
    public PageBean getPostListContent(Integer pageNo, Integer pageSize, Integer sortBy, String searchTerms) {
        PageHelper.startPage(pageNo, pageSize);
        Page<Post> page = adminMapper.getPostListContent(sortBy, searchTerms);

        long total = page.getTotal();
        List<Post> records = page.getResult();

        return new PageBean(total, records);
    }

    //内容管理-video
    @Override
    public PageBean getVideoListContent(Integer pageNo, Integer pageSize, Integer sortBy, String searchTerms) {
        PageHelper.startPage(pageNo, pageSize);
        Page<Video> page = adminMapper.getVideoListContent(sortBy, searchTerms);

        long total = page.getTotal();
        List<Video> records = page.getResult();

        return new PageBean(total, records);
    }

    //内容管理-activity
    @Override
    public PageBean getActivityListContent(Integer pageNo, Integer pageSize, Integer sortBy, String searchTerms) {
        PageHelper.startPage(pageNo, pageSize);
        Page<Activity> page = adminMapper.getActivityListContent(sortBy, searchTerms);

        long total = page.getTotal();
        List<Activity> records = page.getResult();

        return new PageBean(total, records);
    }

}
